package scsys.webServlet.main;

import com.alibaba.fastjson.JSONObject;
import scsys.pojo.*;
import scsys.servlet.CourseService;
import scsys.servlet.CourseServiceImpl;
import scsys.servlet.EletiveService;
import scsys.servlet.EletiveServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet("/delete_course_Servlet")
public class delete_course_Servlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CourseService courseService = new CourseServiceImpl();
        EletiveService eletiveService = new EletiveServiceImpl();


        //1. 接收数据
        BufferedReader br = request.getReader();
        String params = br.readLine();//json字符串
        //转为User对象
        List<chooseCourse> chooseCourses = JSONObject.parseArray(params, chooseCourse.class);
        try {
            for (int i = 0; i < chooseCourses.size(); i++) {
                Course sele_course = new Course();
                Elective sele_elective = new Elective();
                sele_elective.setCourseId(chooseCourses.get(i).getCourse_id());
                sele_course.setId(chooseCourses.get(i).getCourse_id());
                eletiveService.deleteElective(sele_elective);
                courseService.deleteCourse(sele_course);
            }
            response.getWriter().write("yes");
        } catch (IOException e) {
            response.getWriter().write("no");
            e.printStackTrace();
        }


        //3. 写信号
        response.setContentType("text/json;charset=utf-8");

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

}
